package org.example.service;


import org.example.mapper.UserMapper;
import org.example.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Service
public class UserService {


    @Autowired
    private UserMapper userMapper;

    public  int insert(User user) {
        return userMapper.insertOne(user);
    }

    public int delete(int id) {
        return userMapper.deleteOne(id);
    }


    public int update(User user) {
        return userMapper.updateOne(user);
    }




    public List<User> add() {
        return userMapper.selectAll();
    }

    public User select(int id){
        return userMapper.selectOne(id);
    }

    public List<User> selectOrderBy(String orderBy) {
        return userMapper.selectOrderBy(orderBy);
    }

    public List<User> selectLike(String username) {
        return userMapper.selectLike(username);
    }

    public List<User> selectDuoyu() {
        return userMapper.selectDuoyu();
    }

    public List<User> selectUsersAndArticles() {
        return userMapper.selectUsersAndArticles();
    }

    @Transactional(propagation = Propagation.NESTED)
    public int update(boolean error){
        User u = null;
        try {
            u = new User();
            u.setId(1);
            u.setNickname("test, 修改的名称");
            int i = 1/0;
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus()//获取当前事务的状态对象，然后方便进行回滚等操作
                    .setRollbackOnly();//设置事务回滚点
        }
        return userMapper.updateOne(u);
    }
}
